﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading;

namespace ArtMath.Resource
{
    public static class Resources
    {
        static ResourceManager resourceMan;
        static readonly HashSet<string> needNotTranslate = new HashSet<string>() { "xOy", "Ox" };
        public static ResourceManager ResourceManager
        {
            get
            {
                if (resourceMan == null)
                {
                    resourceMan = new ResourceManager(typeof(Resources).FullName, typeof(Resources).Assembly);
                }
                return resourceMan;
            }
        }
        public static string Translate(string text)
        {
            if (needNotTranslate.Contains(text))
            {
                return text;
            }
            return ResourceManager.GetString(text);
        }
        public static bool TryTranslate(string text, out string result)
        {
            if (needNotTranslate.Contains(text))
            {
                result = text;
                return true;
            }
            try
            {
                result = ResourceManager.GetString(text);
                return true;
            }
            catch (Exception)
            {

            }
            result = null;
            return false;
        }
        public static string TranslateMenuItem(string text, char key)
        {
            string res = Translate(text);
            switch (Thread.CurrentThread.CurrentCulture.Name)
            {
                case "en-US":
                    int index = res.IndexOf(key);
                    if (index >= 0)
                    {
                        return res.Substring(0, index) + "&" + res.Substring(index, res.Length - index);
                    }
                    break;
            }
            return $"{res}(&{key})";
        }
        public static string JoinLocalizedString(params string[] args)
        {
            return GetLocalizedString(GetFormatString(null, args.Length), args);
        }
        public static string GetFormatString(string format, int length)
        {
            if (format == null)
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < length; i++)
                {
                    sb.Append("{" + i + "}");
                    if (i < length - 1)
                    {
                        sb.Append(Join);
                    }
                }
                return sb.ToString();
            }
            return format.Replace("+", Join);
        }
        public static string GetLocalizedString(string format, params string[] args)
        {
            string[] trans = new string[args.Length];
            for (int i = 0; i < trans.Length; i++)
            {
                trans[i] = Translate(args[i]);
            }
            if (format == null)
            {
                return trans[0];
            }
            return string.Format(format, trans);
        }
        /// <summary>
        /// 获取逗号分隔表达式
        /// </summary>
        public static string CommaSeparate
        {
            get
            {
                switch (Thread.CurrentThread.CurrentCulture.Name)
                {
                    case "zh-CN":
                        return "，";
                    default:
                        return ", ";
                }
            }
        }
        public static string FullStop
        {
            get
            {
                switch (Thread.CurrentThread.CurrentCulture.Name)
                {
                    case "zh-CN":
                        return "。";
                    default:
                        return ". ";
                }
            }
        }
        /// <summary>
        /// 获取冒号分隔表达式
        /// </summary>
        public static string ColonSeparate
        {
            get
            {
                switch (Thread.CurrentThread.CurrentCulture.Name)
                {
                    case "zh-CN":
                        return "：";
                    default:
                        return ": ";
                }
            }
        }
        /// <summary>
        /// 获取单词间的连接符
        /// </summary>
        public static string Join
        {
            get
            {
                switch (Thread.CurrentThread.CurrentCulture.Name)
                {
                    case "en-US":
                        return " ";
                    default:
                        return string.Empty;
                }
            }
        }
        /// <summary>
        /// 获取左小（圆）括号
        /// </summary>
        public static char LeftParenthesis
        {
            get
            {
                switch (Thread.CurrentThread.CurrentCulture.Name)
                {
                    case "zh-CN":
                        return '（';
                    default:
                        return '(';
                }
            }
        }
        /// <summary>
        /// 获取左中（方）括号
        /// </summary>
        public static char LeftSquareBracket
        {
            get
            {
                switch (Thread.CurrentThread.CurrentCulture.Name)
                {
                    case "zh-CN":
                        return '【';
                    default:
                        return '[';
                }
            }
        }
        /// <summary>
        /// 获取左大（花）括号
        /// </summary>
        public static char LeftBrace
        {
            get
            {
                return '{';
            }
        }
        /// <summary>
        /// 获取右小（圆）括号
        /// </summary>
        public static char RightParenthesis
        {
            get
            {
                switch (Thread.CurrentThread.CurrentCulture.Name)
                {
                    case "zh-CN":
                        return '）';
                    default:
                        return ')';
                }
            }
        }
        /// <summary>
        /// 获取右中（方）括号
        /// </summary>
        public static char RightSquareBracket
        {
            get
            {
                switch (Thread.CurrentThread.CurrentCulture.Name)
                {
                    case "zh-CN":
                        return '】';
                    default:
                        return ']';
                }
            }
        }
        /// <summary>
        /// 获取右大（花）括号
        /// </summary>
        public static char RightBrace
        {
            get
            {
                return '}';
            }
        }
        public static string GetOrdinal(int num)
        {
            switch (Thread.CurrentThread.CurrentCulture.Name)
            {
                case "en-US":
                    switch (num)
                    {
                        case 1:
                            return "1st";
                        case 2:
                            return "2nd";
                        case 3:
                            return "3rd";
                        default:
                            return num + "th";
                    }
                default:
                    return "第" + num;
            }
        }
    }
}
